﻿using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Linq;
using Noodles.Core.Model;
using System;
using System.Xml.Serialization;
using System.IO;
using System.Web.Hosting;
using NHibernate.Criterion;
using NHibernate.Hql;

namespace Noodles.Core.Repositories
{
    class NoodlesRepository : INoodlesRepository
    {
        private readonly ISession session;

        public NoodlesRepository(ISession _session)
        {
            session = _session;
        }
        public IEnumerable<Categories> GetCategories()
        {
            IEnumerable<Categories> categories = session.QueryOver<Categories>().List();
            return categories;
        }
        public IEnumerable<Pages> GetPages()
        {
            IEnumerable<Pages> pagelist = session.QueryOver<Pages>().List();
            return pagelist;
        }
        public IEnumerable<SiteSettings> GetSiteSettings()
        {
            IEnumerable<SiteSettings> settings = session.QueryOver<SiteSettings>().List();
            return settings;
        }
        public PagedData<BlogPost> GetPagedBlog(string page, int pagenumber, int PageSize)
        {
            var blogs = new PagedData<BlogPost>();           
            IList<int> pagenum = session.QueryOver<Pages>().Where(u => u.PageName == page).Select(u => u.PageID).List<int>();
            int pageid = pagenum[0];

            var total = session
               .QueryOver<BlogPost>()
               .RowCount();

                blogs.Data = session
                   .QueryOver<BlogPost>()
                   .Where(u => u.PageID == pageid)
                   .OrderBy(x => x.CreateDate).Desc
                    .Skip(PageSize * (pagenumber - 1))
                    .Take(PageSize)                  
                   .List();

                blogs.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)total / PageSize));
                blogs.CurrentPage = pagenumber;
           
            return blogs;

        }
        public PagedData<BlogPost> GetPagedData(int page)
        {
            int PageSize = 5;
            var blogs = new PagedData<BlogPost>();
            var total = session
               .QueryOver<BlogPost>()
               .RowCount();
            
                
            blogs.Data = session
               .QueryOver<BlogPost>()
               .OrderBy(x => x.CreateDate).Desc
               .Skip(PageSize * (page - 1))
               .Take(PageSize)
               .List();

            blogs.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)total / PageSize));
            blogs.CurrentPage = page;
            return blogs;

        }
        public IEnumerable<BlogPost> GetBlogFromTitle(string Title)
         {          

            var blogs = session.CreateCriteria<BlogPost>()              
                  .Add(Restrictions.Like("Title", Title))
                  .SetFirstResult(0)
                  .List<BlogPost>();

            return blogs;
        }

        public IEnumerable<Comments> GetCommentsFromBlogID(int blogID)
        {
            var comments = session.CreateCriteria<Comments>()
                  .Add(Restrictions.Eq("BlogID", blogID))
                  .List<Comments>();
            return comments;
        }

    }
}
